package zjc.bookkeeping.minepage.aAssetaccount;

import static com.mob.MobSDK.getContext;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import zjc.bookkeeping.PhotoRecognition.addPhotoConsumptionActivity;
import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.adapter.ConsumptionListAdapter;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.homepage.ShowConsumptionList;
import zjc.bookkeeping.homepage.addpage.AddActivity;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.tools.ToolsClass;

public class AssetaccountdetailActivity extends AppCompatActivity {
    ListView consumptionList;
    List<ConsumptionBean> consumptionBeanList;
    ConsumptionListAdapter consumptionListAdapter;
    TextView title,ye;
    int extraid;
    String extratitle;
    String extraye;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_assetaccountdetail);

        //修改详情展示
        Modifydetail();
        consumptionList = findViewById(R.id.zhlist);
        loadList();
    }

    private void Modifydetail() {
        title = findViewById(R.id.asdtitle);
        ye = findViewById(R.id.zhye);
        //获取传递的字符串值
        Intent intent = getIntent();
        extraid= intent.getIntExtra("accountid",0);
        extratitle = intent.getStringExtra("title");
        extraye = intent.getStringExtra("account");
        title.setText(extratitle);
        ye.setText(extraye);
    }

    //返回资产账户页面
    public void back(View v) {
        finish();
//        Intent intent = new Intent(this, AssetaccountActivity.class);
//        startActivity(intent);
    }

    //前往新增页面,还为传入指定账户
    public void addnew(View v) {
        StaticData.setMethodName(extratitle);
        Intent intent = new Intent(AssetaccountdetailActivity.this, AddActivity.class);
        startActivity(intent);
    }

    //加载消费信息列表
    public void onResume() {
        super.onResume();
        loadList();
        consumptionListAdapter.notifyDataSetChanged();
        consumptionList.setAdapter(consumptionListAdapter);
        ye.setText(DBManager.getMethodbyid(extraid).get(0).getMethod_money()+"");
        Log.i("setAdapter", "被调用");
    }

    private void loadList() {
        consumptionBeanList = new ArrayList<>();
        List<ConsumptionBean> list = DBManager.getAllAccountConsumptionbyaccount( extratitle, "");
        Log.i("list.size()", String.valueOf(list.size()));
        for (int i = 0; i < list.size(); i++) {
            Log.i("list", list.get(i).getConsumption_method());
        }
        consumptionBeanList.addAll(list);
        consumptionListAdapter = new ConsumptionListAdapter(getContext(), consumptionBeanList);
    }

    //下拉框
    public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.assetaccountdetail_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.revise:
                        finish();
                        Intent intent = new Intent(getContext(), AssetreviseaccountActivity.class);
                        intent.putExtra("accountid",extraid);
                        System.out.println("2.extraid:"+extraid);
                        startActivity(intent);
                        break;
                    case R.id.delete:
                        finish();
                        DBManager.deleteMethodbyid(extraid);
                        Toast.makeText(AssetaccountdetailActivity.this, "删除成功，请下拉刷新", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        popup.show();
    }

}